package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("/admin/product")
public class SpuManagerController {

    @Autowired
    private ManagerService managerService;


    /**
     admin/product/saveSpuInfo
     * 保存spuInfo

     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){

        //调用Service层的方法实现功能
        managerService.saveSpuInfo(spuInfo);
        //保存就不需要再返回给前端数据了！
        return Result.ok();
    }
    /*@PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){

        managerService.saveSpuInfo(spuInfo);

        return Result.ok();
    }*/

    /**
     获取销售属性数据
     * admin/product/baseSaleAttrList

     */
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){

        //调用Service层的方法实现功能
        List<BaseSaleAttr> baseSaleAttrList = managerService.baseSaleAttrList();

        return Result.ok(baseSaleAttrList);
    }
    /*@GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){

        List<BaseSaleAttr> baseSaleAttrList=managerService.baseSaleAttrList();

        return Result.ok(baseSaleAttrList);
    }*/

    /**
     查询spu列表
     * /admin/product/{page}/{limit}
     * http://localhost/admin/product/1/10?category3Id=61
     * 1.参数列表声明
     *  (Long category3Id)
     * 2.实体类
     *   属性名必须一直
     * 3.请求对象

     */
    @GetMapping("/{page}/{limit}")
    public Result getSpuPageList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo){

        //封装分页条件对象
        Page<SpuInfo> spuInfoPage = new Page<>(page, limit);

        //调用service层方法 执行查询
        IPage<SpuInfo> spuInfoIPage = managerService.getSpuPageList(spuInfoPage, spuInfo);
        return Result.ok(spuInfoIPage);
    }

}
